package org.jiangpeng.customer.util;


/***
 * 转型操着类库
 * @author jiangpeng
 */
public final class CastUtil {

    /**
     * 转为String类型
     * @param obj
     * @param defaultValue
     * @return
     */
    public static  String castString(Object obj, String defaultValue){
        return obj != null ? String.valueOf(obj): defaultValue;
    }

    /**
     * 转为String类型
     * @param object
     * @return
     */
    public  static String castString(Object object){
        return CastUtil.castString(object, "");
    }

    /**
     * 转换为double类型
     * @param object
     * @param defaultValue
     * @return
     */
    public static double castDuble(Object object, double defaultValue){
        double doubleValue = defaultValue;
        if(object != null){
            String strValue = castString(object);
            if(StringUtil.isNotEmpty(strValue)){
                doubleValue = Double.valueOf(strValue);
            }
        }
        return doubleValue;
    }

    /**
     * 转换为double类型
     * @param object
     */
    public static double castDuble(Object object){
        return CastUtil.castDuble(object, 0);
    }

    /**
     * 转为long类型
     * @param object
     * @param defaultValue
     * @return
     */
    public static long castLong(Object object, long defaultValue){
        long longValue = defaultValue;
        if(object != null){
            String stringValue = castString(object);
            if(StringUtil.isNotEmpty(stringValue)){
                longValue = Long.valueOf(stringValue);
            }
        }
        return defaultValue;
    }

    /**
     * 转为long类型
     * @param object
     * @return
     */
    public static long castLong(Object object){
        return castLong(object, 0);
    }

    /**
     * 转换为int类型
     * @param object
     * @param defaultValue
     * @return
     */
    public  static int castInt(Object object, int defaultValue){
        int intValue = defaultValue;
        if(object != null){
            String stringValue = castString(object);
            if(StringUtil.isNotEmpty(stringValue)){
                intValue = Integer.valueOf(stringValue);
            }
        }
        return intValue;
    }

    /**
     * 转换为int类型
     * @param object
     * @return
     */
    public static int castInt(Object object){
        return castInt(object, 0);
    }

    /**
     * 转换为Boolean类型
     * @param object
     * @param defaultValue
     * @return
     */
    public static boolean castBoolean(Object object,boolean defaultValue){
        boolean booleanValue = defaultValue;
        if (object != null){
            String stringValue = castString(object);
            if(StringUtil.isNotEmpty(stringValue)){
                booleanValue = Boolean.valueOf(stringValue);
            }
        }
        return booleanValue;
    }

    public  static  boolean castBoolean(Object object){
        return castBoolean(object, false);
    }
}
